import akshare as ak
import pandas as pd  # 必须导入pandas

df = ak.stock_zh_a_spot_em()

for symbol in df['代码']:
    print(symbol)  # 这里可以替换为你的业务逻辑（如用yfinance获取数据）
    stock_zh_a_hist_fq_df = ak.stock_zh_a_hist(symbol=symbol, period="daily", start_date="20200101", end_date='20200131', adjust="qfq")
    if not stock_zh_a_hist_fq_df.empty:
      stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=symbol, period="daily", start_date="20200101", end_date='20200131', adjust="")
      renamed_df = pd.DataFrame({
       'symbol': stock_zh_a_hist_df['股票代码'],       # 新列名'symbol'对应原列'代码'
       'date': stock_zh_a_hist_df['日期'] ,   # 新列名'stock_name'对应原列'名称'
        'open': stock_zh_a_hist_df['开盘'],
        'high': stock_zh_a_hist_df['最高'],
        'low': stock_zh_a_hist_df['最低'],
        'close': stock_zh_a_hist_df['收盘'],
        'volume': stock_zh_a_hist_df['成交量'],
      })
      adj = pd.DataFrame({
       'date': stock_zh_a_hist_fq_df['日期'] ,   # 新列名'stock_name'对应原列'名称'
        'adjclose': stock_zh_a_hist_fq_df['收盘'],
      })
      result = pd.merge(renamed_df, adj, on='date', how='left')
      result.to_csv(f'../test_data/{symbol}.csv', index=False)
